import {
  queryMaterialLower, queryMaterialLowerNew
} from '@/api/basicManagement/createMateriel'
import { queryTBdMaterialList } from '@/api/basicManagement/materielList'
import { Disable, toMxAmina } from '@/components/ToMxamineState'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'

export default {
  components: {
    search
  },
  mixins: [searData],
  data() {
    return {
      showDis: {},
      ftype: 0,
      aotuTab: false,
      fcontrol: false,
      total: 0,
      pageNum: 1,
      pageSize: 10,
      materialTableData: [], // 物料型號
      // 自动填充
      istoMaterlDialog: false, // 填充物料
      fillMater: {
        total: 0,
        pageNum: 1,
        pageSize: 10
      },
      fillData: [],
      fillHeader: [
        { label: this.$t('market.fmaterial'), prop: 'fnumber', minWidth: '120px' },
        { label: this.$t('shop.fname2'), prop: 'fname', minWidth: '120px' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', minWidth: '120px' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '300px' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecificaTion', minWidth: '100px' }
      ]
    }
  },
  methods: {
    // 1234级物料
    oneMater(ev) {
      if (ev.fnumber) {
        this.materLength[this.indexSelf] = {
          fid: ev.fid,
          fname: ev.fname,
          concats: ev.concats,
          grade: this.indexSelf
        }
        this.materLength.map((item, index) => {
          if (index > this.indexSelf) {
            item.fname = ''
            item.concats = ''
            item.fid = 0
          }
        })
      }
      this.profid = ev.fid
      this.fcontrol = ev.fcontrol // 是否管控
      this.fspecjoinid = ev.fspecjoinid
      this.fattrjoinid = ev.fattrjoinid
      this.fattstatus = ev.fattstatus
      // 物料名称
      this.basicValue.fnames = this.materLength?.[2]?.fname + '(' + this.materLength?.[3]?.fname + ')'
      this.finance.fcategory = ev.fcategoryname || this.finance.fcategory
      this.finance.fcategoryId = ev.fcategoryid || this.finance.fcategoryId
      this.finance.fmaterialGroup = ev.fmaterialgroup || this.finance.fmaterialGroup
      this.finance.fmaterialGroupName = ev.fmaterialgroupname || this.finance.fmaterialGroupName
      this.information.fstock = ev.fstockname || this.information.fstock
      this.information.fstockId = ev.fstockid || this.information.fstockId
      if (ev.fproductstatus) { // 产品
        this.showDis = {
          ...this.showDis,
          flength: ev.flength,
          fwidth: ev.fwidth,
          fheight: ev.fheight,
          fthickness: ev.fthickness,
          fvolume: ev.fvolume,
          fopenlength: ev.fopenlength,
          fopenwidth: ev.fopenwidth,
          fopenheight: ev.fopenheight,
          fopenthickness: ev.fopenthickness,
          fopenvolume: ev.fopenvolume
        }
      }
      if (ev.fpackingstatus) { // 包装
        this.showDis = {
          ...this.showDis,
          fpacklength: ev.fpacklength,
          fpackwidth: ev.fpackwidth,
          fpackheight: ev.fpackheight
        }
      }
      this.oneMateriel = false
      this.disLength() // 尺寸
      // this.handleControl()
    },
    // 物料型号(助记码
    querySearch4(queryString, cb) {
      const restaurants = this.materialTableData
      const results = queryString ? restaurants.filter(this.createFilter4(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter4(queryString) {
      return (restaurant) => {
        return (restaurant.fmodel.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    // 获取物料列表
    async queryTBdMaterialList() {
      const {
        largeCode,
        middleCode,
        smallCode,
        thinCode
      } = this.oneMaterialValue || this.organizationValue
      const DATA = {
        fnumberGroup: largeCode + '-' + middleCode + '-' + smallCode + '-' + thinCode,
        fmodel: this.basicValue.fmodel,
        pageNum: this.pageNum, // 当前页
        pageSize: this.pageSize // 每页显示多少条数据
      }
      const { data: RES } = await queryTBdMaterialList(DATA)
      this.materialTableData = RES.array.map(item => {
        return (toMxAmina(item), Disable(item))
      })
      this.total = RES.total
    },
    // 搜索
    searchMaterialList() {
      this.pageNum = 1
      this.queryTBdMaterialList()
    },
    // 单机获取型号
    material(ev) {
      if (ev.fmodel) {
        this.basicValue.fmodel = ev.fmodel
      }
    },
    // 按钮查询列表(新)
    materialLower() {
      const DATA = {
        largeCode: this.oneMaterialValue.largeCode,
        middleCode: this.oneMaterialValue.middleCode,
        smallCode: this.oneMaterialValue.smallCode,
        thinCode: this.oneMaterialValue.thinCode,
        fmodel: this.basicValue.fmodel,
        ...this.fillMater,
        search: this.searCollData
      }
      queryMaterialLower(DATA).then(res => {
        if (res.code === 0) {
          const dataLength = res.data.array.length
          if (dataLength === 1) {
            this.fillSelectRow(res.data.array[0])
          } else {
            this.istoMaterlDialog = true
            this.fillData = res.data.array
            this.fillMater.total = res.data.total
          }
        } else {
          this.$message({ showClose: true, message: res.message, type: 'success' })
        }
      })
    },
    // 按钮查询获取物料属性(新
    fillSelectRow(item) {
      this.istoMaterlDialog = false
      queryMaterialLowerNew({ fnumber: item.fnumber }).then(res => {
        if (res.code === 0) {
          res.data.data.forEach(item => {
            this.materielProperty.map(ite => {
              if (ite.fname === item.fname) {
                ite.value = item.value
              }
            })
          })
        } else {
          this.$message({ showClose: true, message: res.message, type: 'success' })
        }
      })
    }
  }
}
